﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RestaurantPOSManager.DatabaseTableAdapters;

namespace RestaurantPOSManager
{
    public partial class FrmFoodManager : Form
    {
        public FrmFoodManager()
        {
            InitializeComponent();
        }

        private void FrmFoodManager_Load(object sender, EventArgs e)
        {
            tbl_FoodTableAdapter.Fill(this.database.tbl_Food);
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            string foodCode = txtFoodCode.Value.ToString().Trim();
            string foodName = txtFoodName.Value.ToString().Trim();
            string foodUnit = txtUnit.Value.ToString().Trim();
            string foodPrice = txtPrice.Value.ToString().Trim();
            
            if (foodCode != "" && foodName != "" && foodUnit != "" && foodPrice != "")
            {
                var que = new QueriesTableAdapter();
                que.sp_InsertFood(foodCode, foodName, foodUnit, double.Parse(foodPrice));
            }
            else
            {
                MessageBox.Show("Phải nhập đầy đủ dữ liệu các ô.", "Lỗi nhập liệu", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }

            tbl_FoodTableAdapter.Fill(database.tbl_Food);
        }

        private void gridFood_BeforeDelete(object sender, C1.Win.C1TrueDBGrid.CancelEventArgs e)
        {
            try
            {
                int foodID = int.Parse(gridFood[gridFood.Row, 0].ToString());

                var que = new QueriesTableAdapter();
                que.sp_DeleteFood(foodID);

                tbl_FoodTableAdapter.Fill(database.tbl_Food);
            }
            catch {}
        }
    }
}
